package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class FolderPeer extends BasePeer{
	public static final String tableName = "Folder";
	public static final String FOLDERID = "folderId";
	public static final String FOLDERNAME = "folderName";
	public static final String PARENTID = "parentId";
	public static final String USERID = "userId";
	public static final String POSTCOUNT = "postCount";
	public static final String CREATETIME = "createTime";
	public static final String ABSOLUTEPATH = "absolutePath";
	public static final String[] columnNames=new String[]{"folderId","folderName","parentId","userId","postCount","createTime","absolutePath"};
	public static Folder getFolderByPKs(Object folderId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,FolderPeer.columnNames," TRUE  AND `folderId`=?  AND `userId`=? ",new Object[]{folderId,userId,},0, 1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static Folder getFolderReferenceColumnsOnlyByPKs(Object folderId,Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,new String[]{"folderId","userId",}," TRUE AND `folderId`=?AND `userId`=?",new Object[]{folderId,userId,}, 0,  1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static Folder getFolderByFolderId(long folderId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,FolderPeer.columnNames,"`folderId`=?",new Object[]{folderId},0, 1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static Folder getFolderByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,FolderPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static Folder getFolderByParentId(long parentId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,FolderPeer.columnNames,"`parentId`=?",new Object[]{parentId},0, 1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static Folder getFolderByFolderName(java.lang.String folderName, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(FolderPeer.tableName,FolderPeer.columnNames,"`folderName`=?",new Object[]{folderName},0, 1,conn);
			Folder folder = new Folder();
		fill(rs,folder);
		return folder;
	}
	public static void fill(ResultSet rs, Folder folder) throws SQLException {
		if(rs.next()){
			try{folder.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{folder.setFolderName(rs.getString("folderName"));}catch(Exception ex){}
			try{folder.setParentId(rs.getLong("parentId"));}catch(Exception ex){}
			try{folder.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{folder.setPostCount(rs.getLong("postCount"));}catch(Exception ex){}
			try{folder.setCreateTime(rs.getTimestamp("createTime"));}catch(Exception ex){}
			try{folder.setAbsolutePath(rs.getString("absolutePath"));}catch(Exception ex){}
			folder.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<Folder> folderCollection) throws SQLException {
		while(rs.next()){
			Folder folder=new Folder();
			try{folder.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{folder.setFolderName(rs.getString("folderName"));}catch(Exception ex){}
			try{folder.setParentId(rs.getLong("parentId"));}catch(Exception ex){}
			try{folder.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{folder.setPostCount(rs.getLong("postCount"));}catch(Exception ex){}
			try{folder.setCreateTime(rs.getTimestamp("createTime"));}catch(Exception ex){}
			try{folder.setAbsolutePath(rs.getString("absolutePath"));}catch(Exception ex){}
			folder.resetModified();
			folderCollection.add(folder);
		}
	}
	public static boolean update(Folder folder, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < FolderPeer.columnNames.length; i++) {
			if (((FolderBase)folder).modified[i]) {
				columns.add(FolderPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `Folder` SET ");
		sql.append("WHERE TRUE AND `folderId` = ? AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,folder.get(column));
		}
		pstmt.setObject(columns.size()+1+0,folder.get("folderId"));
		pstmt.setObject(columns.size()+1+1,folder.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(Folder folder, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (folder.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `Folder` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (folder.get(column) != null) {
				pstmt.setObject(i + 1 + j, folder.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<Folder> getFolderCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<Folder> collection=new Vector<Folder>();
		fill(rs,collection);
		return collection;
	}
}
